<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg">
  <head>
    <title>Threads</title>
    <link rel="stylesheet" type="text/css" href="docbook-epub.css"/>
    <link rel="stylesheet" type="text/css" href="kawa.css"/>
    <script src="kawa-ebook.js" type="text/javascript"/>
    <meta name="generator" content="DocBook XSL-NS Stylesheets V1.79.1"/>
    <link rel="prev" href="Overall-Index.xhtml" title="Index"/>
    <link rel="next" href="Exceptions.xhtml" title="Exception handling"/>
  </head>
  <body>
    <header/>
    <section class="sect1" title="Threads" epub:type="subchapter" id="Threads">
      <div class="titlepage">
        <div>
          <div>
            <h2 class="title" style="clear: both">Threads</h2>
          </div>
        </div>
      </div>
      <p>There is a very preliminary interface to create parallel threads.
The interface is similar to the standard <code class="literal">delay</code>/<code class="literal">force</code>,
where a thread is basically the same as a promise, except that
evaluation may be in parallel.
</p>
      <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877982720" class="indexterm"/> <code class="function">future</code> <em class="replaceable"><code>expression</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Creates a new thread that evaluates <em class="replaceable"><code>expression</code></em>.
</p>
          <p>(The result extends <code class="literal">java.lang.Thread</code> and
implements <code class="literal">gnu.mapping.Lazy</code>.)
</p>
        </blockquote>
      </div>
      <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877977632" class="indexterm"/> <code class="function">force</code> <em class="replaceable"><code>thread</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>The standard <code class="literal">force</code> function is generalized to also work
on threads.  It waits for the thread’s <em class="replaceable"><code>expression</code></em> to finish
executing, and returns the result.
</p>
        </blockquote>
      </div>
      <p class="synopsis" kind="Procedure"><span class="kind">Procedure</span><span class="ignore">: </span><a id="idm139667877973184" class="indexterm"/> <code class="function">runnable</code> <em class="replaceable"><code>function</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Creates a new <code class="literal">Runnable</code> instance from a function.
Useful for passing to Java code that expects a <code class="literal">Runnable</code>.
You can get the result (a value or a thrown exception) using the
<code class="literal">getResult</code> method.
</p>
        </blockquote>
      </div>
      <p class="synopsis" kind="Syntax"><span class="kind">Syntax</span><span class="ignore">: </span><a id="idm139667877968352" class="indexterm"/> <code class="function">synchronized</code> <em class="replaceable"><code>object</code></em> <em class="replaceable"><code>form</code></em> <em class="replaceable"><code>...</code></em></p>
      <div class="blockquote">
        <blockquote class="blockquote">
          <p>Synchronize on the given <em class="replaceable"><code>object</code></em>.  (This means getting an
exclusive lock on the object, by acquiring its <em class="firstterm">monitor</em>.)
Then execute the <em class="replaceable"><code>form</code></em>s while holding the lock.
When the <em class="replaceable"><code>form</code></em>s finish (normally or abnormally by throwing
an exception), the lock is released.
Returns the result of the last <em class="replaceable"><code>form</code></em>.
Equivalent to the Java <code class="literal">synchronized</code> statement,
except that it may return a result.
</p>
        </blockquote>
      </div>
    </section>
    <footer>
      <div class="navfooter">
        <p>
          Up: <a accesskey="u" href="Program-structure.xhtml">Program structure</a></p>
        <p>
        Previous: <a accesskey="p" href="Lazy-evaluation.xhtml">Lazy evaluation</a></p>
        <p>
        Next: <a accesskey="n" href="Exceptions.xhtml">Exception handling</a></p>
      </div>
    </footer>
  </body>
</html>
